package util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * 给图片添加水印
 * 将图片的字节码传入，添加水印后将字节码返回
 *
 * 导入jar包
 * <dependency>
 *      <groupId>org.projectlombok</groupId>
 *      <artifactId>lombok</artifactId>
 *      <version>1.18.4</version>
 *      <scope>provided</scope>
 * </dependency>
 * <dependency>
 *      <groupId>org.apache.commons</groupId>
 *      <artifactId>commons-lang3</artifactId>
 *      <version>3.3.2</version>
 * </dependency>
 */
@Slf4j
public class ImageUtil {

    private static final String LOGO_TEXT = "这是一个水印";
    private static final Integer DEGREE = -20;
    private static final Integer FONT_SIZE = 18;
    private static final Integer INTERVAL = FONT_SIZE * 6;
    private static final Color COLOR = Color.BLACK;


    /**
     * @param bytes      源图片字节数组
     * @param suffixName 图片后缀名
     */
    public static byte[] markImageByText(byte[] bytes, String suffixName) {
        ByteArrayOutputStream byteArrayOutputStream = null;
        ByteArrayInputStream byteArrayInputStream = null;
        try {
            // 1、源图片
            byteArrayInputStream = new ByteArrayInputStream(bytes);
            java.awt.Image srcImg = ImageIO.read(byteArrayInputStream);
            BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB);
            // 2、得到画笔对象
            Graphics2D g = buffImg.createGraphics();
            // 3、设置对线段的锯齿状边缘处理
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), java.awt.Image.SCALE_SMOOTH), 0, 0, null);
            // 4、设置水印旋转
            g.rotate(Math.toRadians(DEGREE), buffImg.getWidth() / 2, buffImg.getHeight() / 2);
            // 5、设置水印文字颜色
            g.setColor(COLOR);
            // 6、设置水印文字Font
            g.setFont(new Font(null, Font.CENTER_BASELINE, FONT_SIZE));
            // 7、设置水印文字透明度
            g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.2f));
            // 8、第一参数->设置的内容，后面两个参数->文字在图片上的坐标位置(x,y)
            for (int y = -INTERVAL; y < buffImg.getHeight() + INTERVAL; y += INTERVAL) {
                for (int x = -INTERVAL; x < buffImg.getWidth() + INTERVAL; x += (FONT_SIZE * LOGO_TEXT.length() + INTERVAL)) {
                    g.drawString(LOGO_TEXT, x, y);
                }
            }
            // 9、释放资源
            g.dispose();
            // 10、生成图片
            byteArrayOutputStream = new ByteArrayOutputStream();
            ImageIO.write(buffImg, suffixName, byteArrayOutputStream);
            //返回字节对象
            return byteArrayOutputStream.toByteArray();
        } catch (Exception e) {
            log.error("图片添加水印失败:{}", ExceptionUtils.getStackTrace(e));
        } finally {
            try {
                if (null != byteArrayOutputStream) {
                    byteArrayOutputStream.close();
                }
                if (null != byteArrayInputStream) {
                    byteArrayInputStream.close();
                }
            } catch (Exception e) {
                log.error("图片添加水印释放资源失败:{}", ExceptionUtils.getStackTrace(e));
            }
        }
        return null;
    }

    public static void main(String[] args) {
//        markImageByText("编码", "图片后缀");
    }


}
